﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Janus.Windows.EditControls;
using Janus.Windows.GridEX;
using System.Diagnostics;
using System.IO;

namespace VehicleScale
{
    public partial class TemplateForm : Form
    {

        #region Fields
        protected readonly SettingApp Setting = new SettingApp(); 
        #endregion

        #region Ctors

        public TemplateForm()
        {
            InitializeComponent();

            //this.officeFormAdorner1.Office2007CustomColor = Setting.CustomColor;
            //this.officeFormAdorner1.Office2007ColorScheme = Setting.ColorScheme;

            this.KeyDown += new KeyEventHandler(delegate(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Enter)
                    SendKeys.Send("{TAB}");
            });
        }

        protected override void OnLoad(EventArgs e)
        {
            // GUI
            // DoSet(this);
            base.OnLoad(e);
        }

        private void DoSet(Control parentControl)
        {
            foreach (Control item in parentControl.Controls)
            {
                DoSet(item);
            }
        }

        

        #endregion

        #region Methods
        protected bool OnExportExcel(GridEX gridEx)
        {
            bool result = false;
            try
            {
                this.gridEXExporter1.GridEX = gridEx;
                gridEXExporter1.ExportMode = ExportMode.AllRows;
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "All Microsoft Excel files|*.xls|All Microsoft Excel 2007 files|*.xlsx|All Files|*.*";
                saveFileDialog.DefaultExt = "*.xls";
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    if (File.Exists(saveFileDialog.FileName))
                        File.Delete(saveFileDialog.FileName);
                    using (Stream stream = new FileStream(saveFileDialog.FileName, System.IO.FileMode.CreateNew))
                    {
                        this.gridEXExporter1.Export(stream);
                    }
                    Library.Tools.MsgBox.Show("Quá trình xuất file Excel (" + saveFileDialog.FileName + ") thành công.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    result = true;
                }
                try
                {
                    LogFile.WriteLog(LogFile.nameFile, LogFile.userLogin, "Xuat Excel trong Form danh sach Phieu can");
                }
                catch
                {
                }
            }
            catch (Exception ex)
            {
                Library.Tools.MsgBox.Show("Quá trình xuất file Excel bị lỗi: " + ex.Message, "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            return result;
        } 
        #endregion

    }
}
